//
//  UpdateAppAlertView.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/6.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "UpdateAppAlertView.h"

@interface UpdateAppAlertView ()

@property(nonatomic ,strong)UIView *alertView;

@end
@implementation UpdateAppAlertView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        self.backgroundColor = rgba(0, 0, 0, 0.8);
    }
    return self;
}
- (void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    [_alertView removeFromSuperview];
    _alertView = nil;
    [self addSubview:self.alertView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    UITouch *t = [touches anyObject];
//    CGPoint p = [t locationInView:self];
//    if (CGRectContainsPoint(self.alertView.frame, p)) {
//        return;;
//    }
//    [self removeFromSuperview];
}
- (void)cancelBtnClick
{
    [self removeFromSuperview];
}
- (void)updateBtnBtnClick
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/idxxxxxx"] options:@{} completionHandler:^(BOOL success) {
                        
            }];

}
- (UIView *)alertView
{
    if (!_alertView) {
        _alertView = [[UIView alloc] initWithFrame:CGRectMake(15, 1, lScreenWidth - 30, 345)];
        _alertView.backgroundColor = lWhiteColor;
        _alertView.centerY = self.centerY;
        [_alertView setAllCornersWithCornerRadius:15];
        UIImageView *lampView = [[UIImageView alloc] initWithFrame:CGRectMake((lScreenWidth - 96)/2.0, 60, 76, 86)];
        lampView.image = IMAGENAMED(@"ic_update_alert");
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(10,lampView.bottom + 30,SCREEN_W - 20,32);
        label.numberOfLines = 1;
        [_alertView addSubview:label];
        
        label.text =  [NSString stringWithFormat:@"更新版本 V%@",self.dic[@"version"]];
        label.textColor = rgba(51, 51, 51, 1);
        label.font = MFontWithSize(16);
        label.textAlignment = NSTextAlignmentCenter;
        [_alertView addSubview:lampView];
        NSArray *array = @[];
        if (self.dic[@"content"]) {
            array = @[self.dic[@"content"]];
        }
        double y = label.bottom;
       // double x = 0;
        for (int i=0; i < array.count; i++) {
           
            UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(96, label.bottom + 15 + (26 + 10)*i, lScreenWidth - 33 - 20, 26)];
            lab.text = array[i];
            lab.textColor = rgba(142, 142, 142, 1);
            lab.textAlignment = NSTextAlignmentLeft;
            double w = [MethodTool getTextWidthWithStr:lab.text font:lab.font ViewHeight:lab.height];
            w = w > lab.width ? lab.width:w;
            lab.width = w;
            lab.centerX = _alertView.centerX;
            
//            if (i > 0) {
//            lab.x = x;
//            }else{
//            x = lab.x;
//            }
            UIView *pot = [[UIView alloc] initWithFrame:CGRectMake(lab.x - 13, lab.y + 10, 6, 6)];
            pot.backgroundColor = rgba(142, 142, 142, 1);
            [pot setAllCornersWithCornerRadius:3];
            [_alertView addSubview:lab];
            [_alertView addSubview:pot];
            y = lab.bottom;
            
        }
        double w = (lScreenWidth - 75 - 20)/2.0;
        UIButton *cancelBtn = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(25, y + 30, w, 45);
            [button setTitle:@"暂不更新" forState:UIControlStateNormal];
            button.titleLabel.font = MFontWithSize(16.f);
            [button setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
            [button addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [button setAllCornersWithCornerRadius:22.5];
            button.layer.borderColor = rgba(51, 51, 51, 1).CGColor;
            button.layer.borderWidth = 0.5;
            button;
        });
        
        
        UIButton *updateBtn = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(cancelBtn.right + 25, y + 30, w, 45);
            [button setTitle:@"立即更新" forState:UIControlStateNormal];
            button.titleLabel.font = MFontWithSize(16.f);
            [button setTitleColor:lWhiteColor forState:UIControlStateNormal];
            [button addTarget:self action:@selector(updateBtnBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [button setAllCornersWithCornerRadius:22.5];
            button;
        });
        if ([self.dic[@"enforce"] doubleValue] == 1) {
            cancelBtn.hidden = YES;
            updateBtn.centerX = (lScreenWidth - 30)/2.0;
        }
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = updateBtn.frame;
        gl.startPoint = CGPointMake(1, 0.5);
        gl.endPoint = CGPointMake(0, 0.5);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        gl.cornerRadius = 22.5;
        
        [_alertView.layer addSublayer:gl];
        [_alertView addSubview:updateBtn];
        [_alertView addSubview:cancelBtn];
        _alertView.height = cancelBtn.bottom + 20;
        
    }
    return _alertView;
}

@end
